<template>
  <el-collapse-transition>
    <div class="headerSearch" v-show="isShowHeaderSearch" ref="headerSearch">
      <el-form ref="form" :model="formData" :inline="true" size="mini" label-width="120px">
        <template v-for="(item,index) in cFields">
          <el-form-item :label="item.label" :prop="item.prop" :key="index" v-if="item.type">
            <component
              :is="item.type"
              v-model="formData[item.prop]"
              @change="inputFun(item,formData[item.prop])"
              size="mini"
              :clearable="!item.disabled"
              v-bind="item.props"
              :placeholder="item.placeholder"
            >
              <template v-if="item.type==='el-select' && item.options && item.options.length>0">
                <el-option
                  v-for="sitem in item.options"
                  :key="sitem.value"
                  :label="sitem.label"
                  :value="sitem.value"
                  :disabled="item.disabled"
                ></el-option>
              </template>
            </component>
          </el-form-item>
        </template>
      </el-form>
    </div>
  </el-collapse-transition>
</template>

<script>
export default {
  name: "headerSearch",
  props: {
    fields: {
      type: Array,
      default: () => []
    },
    defaultSearchFields: {
      type: Array,
      default: () => []
    }
  },
  computed: {
    cFields () {
      return this.defaultSearchFields.concat(this.fields);
    }
  },
  created () {
    this.$nextTick(() => {
      console.log("xxxsd");
      if (this.defaultSearchFields.length > 0) {
        this.defaultSearchFields.forEach(v => {
          if (v.value) {
            this.$set(this.formData, v.prop, v.value);
          } else {
            this.$set(this.formData, v.prop, "");
          }
        });
      }
    });
    // if (this.fields.length > 0) {
    //   let nameArr = this.fields.filter(v => v.type).map(v => v.prop);
    //   console.log(this.defaultSearchFields);
    //   nameArr.forEach(v => {
    //     if (!this.formData[v]) {
    //       this.$set(this.formData, v, "");
    //     }
    //   });
    // }
  },
  data () {
    return {
      formData: {},
      isShowHeaderSearch: true,
      label: ""
    };
  },
  methods: {
    setIsShowHeader (status) {
      this.isShowHeaderSearch = status;

    },
    resetForm (formName) {
      this.$refs[formName].resetFields();
    },
    onSubmit () {
      console.log(this.formData);
      this.$emit("submit", this.formData);
    },
    inputFun (item, val) {
      // this.$set(this.formData, item.prop, val);
      // console.log(item);
      // console.log(val);
      // console.log(this.formData);
      this.$emit("change", this.formData);
    }
  }
};
</script>

<style lang="less" scoped>
.headerSearch {
  // height: 200px;
  background-color: #fff;
  // display: flex;
  // align-items: center;
  // flex-wrap: nowrap;
  overflow: auto;
  margin-bottom: 10px;
  /deep/ .el-form {
    width: 100%;
    height: 100%;
    // display: flex;
    padding-top: 10px;
    .action-item {
      display: flex;
      align-items: center;
      justify-content: center;
    }
    // align-items: center;
  }
  /deep/ .el-form-item {
    // margin-bottom: 0;
    // display: flex;
    .el-form-item__content {
      width: 200px;
      .el-date-editor.el-input {
        width: 200px;
      }
      .el-select {
        width: 200px;
      }
    }
  }
  /deep/.el-scrollbar .el-scrollbar__wrap .el-scrollbar__view {
    white-space: nowrap;
  }
}
</style>
